/**
 * 
 */
package com.gwcloud.leetcode.offers;

/**
 * @ClassName   : Leetcode0210 
 * @author      : ganwei 
 * @date        : 2020年6月1日
 * @Description : Leetcode0210
 */
public class Leetcode0210 {
	/**
	 * -解题思路，暴力解法
	 */
    public int[] exchange(int[] nums) {
        int[] res = new int[nums.length];
        int rstart = 0, rend = res.length-1;
        for(int i = 0; i < nums.length; i++){
        	if(nums[i] % 2 != 0){
        		res[rstart] = nums[i];
        		rstart++;
        	}else{
        		res[rend] = nums[i];
        		rend--;
        	}
        }
        return res;
    }
    
	/**
	 * -解题思路，双指针
	 */
    public int[] exchange_1(int[] nums){
    	int left = 0, right = nums.length-1;
    	while(left < right){
    		if(nums[left] % 2 != 0){
    			left++;
    			continue;
    		}
    		if(nums[right] % 2 == 0){
    			right--;
    		}

    		
    		int temp = nums[left];
    		nums[left] = nums[right];
    		nums[right] = temp;
    		left++;
    		right--;
    	}
    	return nums;
    }
    public static void main(String[] args){
    	Leetcode0210 l0210 = new Leetcode0210();
    	int[] nums = {1,3,5};
    	for(int num : l0210.exchange_1(nums)){
    		System.out.print(num);
    		System.out.print(" ");
    	}
    }
}
